{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "     年龄  教育  工龄  地址   收入   负债率      信用卡负债      其他负债  违约\n0    41   3  17  12  176   9.3  11.359392  5.008608   1\n1    27   1  10   6   31  17.3   1.362202  4.000798   0\n2    40   1  15  14   55   5.5   0.856075  2.168925   0\n3    41   1  15  14  120   2.9   2.658720  0.821280   0\n4    24   2   2   0   28  17.3   1.787436  3.056564   1\n..   ..  ..  ..  ..  ...   ...        ...       ...  ..\n695  36   2   6  15   27   4.6   0.262062  0.979938   1\n696  29   2   6   4   21  11.5   0.369495  2.045505   0\n697  33   1  15   3   32   7.6   0.491264  1.940736   0\n698  45   1  19  22   77   8.4   2.302608  4.165392   0\n699  37   1  12  14   44  14.7   2.994684  3.473316   0\n\n[700 rows x 9 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>年龄</th>\n      <th>教育</th>\n      <th>工龄</th>\n      <th>地址</th>\n      <th>收入</th>\n      <th>负债率</th>\n      <th>信用卡负债</th>\n      <th>其他负债</th>\n      <th>违约</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>41</td>\n      <td>3</td>\n      <td>17</td>\n      <td>12</td>\n      <td>176</td>\n      <td>9.3</td>\n      <td>11.359392</td>\n      <td>5.008608</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>27</td>\n      <td>1</td>\n      <td>10</td>\n      <td>6</td>\n      <td>31</td>\n      <td>17.3</td>\n      <td>1.362202</td>\n      <td>4.000798</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>40</td>\n      <td>1</td>\n      <td>15</td>\n      <td>14</td>\n      <td>55</td>\n      <td>5.5</td>\n      <td>0.856075</td>\n      <td>2.168925</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>41</td>\n      <td>1</td>\n      <td>15</td>\n      <td>14</td>\n      <td>120</td>\n      <td>2.9</td>\n      <td>2.658720</td>\n      <td>0.821280</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>24</td>\n      <td>2</td>\n      <td>2</td>\n      <td>0</td>\n      <td>28</td>\n      <td>17.3</td>\n      <td>1.787436</td>\n      <td>3.056564</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>695</th>\n      <td>36</td>\n      <td>2</td>\n      <td>6</td>\n      <td>15</td>\n      <td>27</td>\n      <td>4.6</td>\n      <td>0.262062</td>\n      <td>0.979938</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>696</th>\n      <td>29</td>\n      <td>2</td>\n      <td>6</td>\n      <td>4</td>\n      <td>21</td>\n      <td>11.5</td>\n      <td>0.369495</td>\n      <td>2.045505</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>697</th>\n      <td>33</td>\n      <td>1</td>\n      <td>15</td>\n      <td>3</td>\n      <td>32</td>\n      <td>7.6</td>\n      <td>0.491264</td>\n      <td>1.940736</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>698</th>\n      <td>45</td>\n      <td>1</td>\n      <td>19</td>\n      <td>22</td>\n      <td>77</td>\n      <td>8.4</td>\n      <td>2.302608</td>\n      <td>4.165392</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>699</th>\n      <td>37</td>\n      <td>1</td>\n      <td>12</td>\n      <td>14</td>\n      <td>44</td>\n      <td>14.7</td>\n      <td>2.994684</td>\n      <td>3.473316</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n<p>700 rows × 9 columns</p>\n</div>"
     },
     "metadata": {},
     "execution_count": 1
    }
   ],
   "source": [
    "import pandas as pd\n",
    "data=pd.read_excel('./bankloan.xls')\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "x=data.iloc[:,:-1]\n",
    "y=data.iloc[:,-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "逻辑回归"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "0.8285714285714286"
     },
     "metadata": {},
     "execution_count": 4
    }
   ],
   "source": [
    "from sklearn.linear_model import LogisticRegression\n",
    "\n",
    "LR=LogisticRegression()\n",
    "LR.fit(x_train,y_train)\n",
    "LR.score(x_test,y_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "朴素贝叶斯"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "朴素贝叶斯在测试集上的准确率：\n"
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "0.7785714285714286"
     },
     "metadata": {},
     "execution_count": 5
    }
   ],
   "source": [
    "from sklearn.naive_bayes import GaussianNB,BernoulliNB,MultinomialNB\n",
    "gnb=GaussianNB()\n",
    "gnb.fit(x_train,y_train)\n",
    "print(\"朴素贝叶斯在测试集上的准确率：\")\n",
    "gnb.score(x_test,y_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "决策树"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}